---
title: "MongoDB Filters"
date: 2021-2-20
categories:
- mongodb
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org3c6b0b5">查询</a></li>
<li><a href="#orgadce78e">数组查询</a></li>
<li><a href="#org7970fab">空值判断</a></li>
</ul>
</div>
</div>
<blockquote>
<p>
<a href="https://docs.mongodb.com/manual/tutorial/query-documents/">https://docs.mongodb.com/manual/tutorial/query-documents/</a>
</p>
</blockquote>
<div class="outline-2" id="outline-container-org3c6b0b5">
<h2 id="org3c6b0b5">查询</h2>
<div class="outline-text-2" id="text-org3c6b0b5">
<p>
db.inventory.find( { status: "D" } )
SELECT * FROM inventory WHERE status = "D"
</p>
<p>
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
SELECT * FROM inventory WHERE status in ("A", "D")
</p>
<p>
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
SELECT * FROM inventory WHERE status = "A" AND qty &lt; 30
</p>
<div class="org-src-container">
<pre class="src src-js">db.inventory.find({
        $or: [ {
            status: <span style="font-style: italic;">"A"</span>
        }, {
            qty: { $lt: 30 }
        } ]
    } )
</pre>
</div>
<p>
SELECT * FROM inventory WHERE status = "A" OR qty &lt; 30
</p>
<p>
db.inventory.find( {
     status: "A",
     $or: [ { qty: { $lt: 30 } }, { item: <i>^p</i> } ]
} )
SELECT * FROM inventory WHERE status = "A" AND ( qty &lt; 30 OR item LIKE "p%")
</p>
</div>
</div>
<div class="outline-2" id="outline-container-orgadce78e">
<h2 id="orgadce78e">数组查询</h2>
<div class="outline-text-2" id="text-orgadce78e">
<p>
db.inventory.find( { tags: ["red", "blank"] } )
精确匹配，筛选tags值为有且仅有'red' 'black' 两个元素的文档，且顺序一致
</p>
<p>
db.inventory.find( { tags: { $all: ["red", "blank"] } } )
筛选tags的值包含'red' 'blank'两个元素的文档，有其他元素或顺序不一致的文档也匹配
</p>
<p>
db.inventory.find( { tags: "red" } )
这样的查询也能对tags值为数组的文档进行筛选，即数组中至少有一个值为red的文档也匹配。其他的例如：
db.inventory.find( { dim_cm: { $gt: 25 } } )
表示只要数据中一个元素匹配则文档也匹配
</p>
<p>
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )
只要能找到满足两个条件的元素，则匹配。例如数组中两元素，元素a&gt;15而元素b&lt;20，则这个文档匹配。不必一个元素同时15&lt; &lt;20
</p>
<p>
db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
这个查询需要找到一个元素同时满足两个条件时，文档才匹配
</p>
<p>
db.inventory.find( { "dim_cm.1": { $gt: 25 } } )
下标匹配，where dim_cm[1] = 25，下标从0开始
</p>
<p>
db.inventory.find( { "tags": { $size: 3 } } )
数组长度筛选，where len(tags) = 3
</p>
</div>
</div>
<div class="outline-2" id="outline-container-org7970fab">
<h2 id="org7970fab">空值判断</h2>
<div class="outline-text-2" id="text-org7970fab">
<p>
{"name": {$exists: true}}
where document.keys contains 'name' 筛选有name属性的文档 
</p>
<p>
{"name": {$type: 10}}
where document.keys contains 'name' and document.name is null
筛选文档包含name属性，并且值为null，type用于判断值的类型，10表示NULL这个类型
</p>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2021-2-20</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
